﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;


public partial class GiaoDien_DisplayTank : System.Web.UI.MasterPage
{
    static HttpCookie cookie = new HttpCookie("Language");
    ProductBus productBus = new ProductBus();

    protected void Page_Load(object sender, EventArgs e)
    {


        if (!Page.IsPostBack)
        {
            DataTable dt = productBus.GetUrlImgByCategoryName("Tank");
            listView.DataSource = dt;
            listView.DataBind();

            //Su li Cookie
            #region Cookie
            try
            {
                //SetCurrentPage();
                if (cookie.Value == null)
                {
                    Response.Cookies.Add(cookie);
                }

                if (Session["UserName"] != null)
                {
                    lblLoginFromTitle.Text = "Hi " + Session["UserName"];
                    lblLoginFromTitle.Visible = true;
                    txtpassword.Visible = false;
                    txtusername.Visible = false;
                    btnLogin.Visible = false;
                    cbRemember.Visible = false;
                }
                //check remember me cookie
                if (Request.Cookies["RememberMe"] != null)
                {
                    HttpCookie cookieRememberMe = Request.Cookies.Get("RememberMe");
                    txtusername.Text = cookieRememberMe.Values["UserName"].ToString();
                    txtpassword.Attributes.Add("Value", cookieRememberMe.Values["Password"].ToString());

                    if (cookieRememberMe.Values["CheckBox"].ToString() == "True")
                    {
                        cbRemember.Checked = true;
                    }
                    else
                    {
                        cbRemember.Checked = false;
                    }

                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);

            }

        }
            #endregion

        else
        {

            ProcessSearch();

        }

    }

    public void ProcessSearch()
    {
        //su li list check box
        #region Xu li Checkbox

        string txtSize = "";

        for (int i = 0; i < cblist.Items.Count; i++)
        {
            if (cblist.Items[i].Selected == true)
            {
                if (i == 0)
                {
                    txtSize += cblist.Items[i].Value;
                }

                else
                {
                    txtSize += "," + cblist.Items[i].Value;

                }
            }


        }


        string arrColor = "";

        for (int i = 0; i < cbColor.Items.Count; i++)
        {
            if (cbColor.Items[i].Selected == true)
            {
                if (i == 0)
                {
                    arrColor += cbColor.Items[i].Value;
                }
                else
                {
                    arrColor += "," + cbColor.Items[i].Value;
                }
            }

        }

        float price1;
        float price2;

        if (txtToPrice.Text == "")
        {
            price1 = 0;
            price2 = 0;
        }
        else
        {
            price1 = float.Parse(txtToPrice.Text);
            price2 = float.Parse(txtFromPrice.Text);
        }
        if (txtSize == "")
        {
            txtSize = "''";
        }
        if (arrColor == "")
        {
            arrColor = "''";
        }

        DataTable dt1 = productBus.GetProductDetail(txtSize, arrColor, price1, price2, "Tank");

        listView.DataSource = dt1;
        listView.DataBind();

        #endregion
    }

    protected void btnLogin_Click1(object sender, EventArgs e)
    {
        {

            string username = txtusername.Text;
            string password = txtpassword.Text;

            CustomerBus customerBus = new CustomerBus();

            if (customerBus.IsValidLogin(username, password))
            {
                //khai bao bien cookie
                HttpCookie cookieRememberMe = new HttpCookie("RememberMe");

                Session["username"] = username;

                //neu chon check box remember
                if (cbRemember.Checked == true)
                {
                    //xoa cookie cu
                    if (Request.Cookies["RememberMe"] != null)
                    {
                        cookieRememberMe = new HttpCookie("RememberMe");
                        cookieRememberMe.Values.Add("UserName", "");
                        cookieRememberMe.Values.Add("Password", "");
                        cookieRememberMe.Values.Add("CheckBox", "");
                        //gan thoi gian song cua cookie la 30 ngay 
                        cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                        //them cookie
                        Response.Cookies.Add(cookieRememberMe);
                    }

                    //luu cookie
                    cookieRememberMe = new HttpCookie("RememberMe");
                    cookieRememberMe.Values.Add("username", txtusername.Text.Trim());
                    cookieRememberMe.Value.Equals(username);
                    cookieRememberMe.Values.Add("password", txtpassword.Text.Trim());
                    cookieRememberMe.Values.Add("CheckBox", "True");
                    Response.Write(txtusername.Text);
                    //gan thoi gian song cua cookie la 30 ngay 
                    cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                    //them cookie
                    Response.Cookies.Add(cookieRememberMe);
                }
                else
                {
                    //delete cookie
                    if (Request.Cookies["RememberMe"] != null)
                    {
                        cookieRememberMe = new HttpCookie("RememberMe");
                        cookieRememberMe.Values.Add("UserName", txtusername.Text.Trim());
                        cookieRememberMe.Values.Add("Password", "");
                        cookieRememberMe.Values.Add("CheckBox", "");
                        //gan thoi gian song cua cookie la 30 ngay 
                        cookieRememberMe.Expires = DateTime.Now.AddDays(30);
                        //them cookie
                        Response.Cookies.Add(cookieRememberMe);
                    }
                }
            }
            else
            {
                //  Response.Redirect("../GiaoDien/Register.aspx");
                //lblLoginFail.Text = "Invalid username or password !";
                //lblLoginFail.Visible = true;
            }
        }
    }

    protected void cblist_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
